package com.aiwhose.aiwhosefile.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aiwhose.aiwhosecore.bean.Result;
import com.aiwhose.aiwhosecore.bean.file.File;
import com.aiwhose.aiwhosefile.service.FileService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Map;

@CrossOrigin
@SaCheckLogin
@RestController
@RequestMapping("/qiniuyun")
public class QiniuyunController {
    @Resource
    FileService fileService;

    @PostMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("files") MultipartFile[] files, String bucket, Integer time, HttpServletRequest request) throws Exception {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String loginId = (String) tokenInfo.loginId;
        ArrayList<File> files1 = fileService.uploadFile(files, loginId, bucket);

        return Result.ok(files1);
    }

    @PostMapping("/wxMiniAppUpload")
    @ResponseBody
    public Result wxMiniAppUpload(@RequestParam("file") MultipartFile[] file, String bucket, Integer time, HttpServletRequest request) throws Exception {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String loginId = (String) tokenInfo.loginId;
        ArrayList<File> files = fileService.uploadFile(file, loginId, bucket);
        return Result.ok(files);
    }

    @PostMapping("/reUpload")
    @ResponseBody
    public Result upload(@RequestParam("files") MultipartFile[] files, String bucket, String path, HttpServletRequest request) throws IOException {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String loginId = (String) tokenInfo.loginId;
        return fileService.reUploadFile(files, loginId, path, bucket);
    }


    //    下载文件
    @PostMapping("/downloadFile")
    @ResponseBody
    public Result downloadFile(@RequestBody Map<String, Object> body) throws Exception {
        String path = MapUtil.get(body, "path", String.class, "");
        Long time = MapUtil.get(body, "time", Long.class, 60 * 60L);
        String bucket = MapUtil.get(body, "bucket", String.class, "private");
        return fileService.downloadFile(path, time, bucket);
    }

    //    获取地址
    @GetMapping("/getPrivateUrl")
    @ResponseBody
    @SaIgnore
    public String getPrivateUrl(@RequestParam("id") String id) throws Exception {
        File file = fileService.getById(id);
        return fileService.getPrivateUrl(file.getUrl());
    }

    //    删除文件
    @PostMapping("/deletedFile")
    @ResponseBody
    public Result deletedFile(@RequestBody Map<String, Object> body) {
        String[] files = MapUtil.get(body, "files", String[].class);
        String bucket = MapUtil.get(body, "bucket", String.class, "public");
        return fileService.deleteFile(files, bucket);
    }

    /**
     * 获取文件列表
     *
     * @param body prefix: 目录
     *             maxLimit: 每次迭代的长度限制，最大1000，推荐值 1000
     * @return
     * @throws Exception
     */
    @PostMapping("/select")
    @ResponseBody
    public Result getFileList(@RequestBody Map<String, Object> body) throws Exception {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String loginId = (String) tokenInfo.loginId;
        String prefix = loginId + "/";
        String year = MapUtil.get(body, "year", String.class, "");
        String month = MapUtil.get(body, "month", String.class, "");
        String day = MapUtil.get(body, "day", String.class, "");
        if (ObjectUtil.isNotEmpty(day)) {
            prefix = prefix + formatDate(day, "yyyy/MM/dd");
        }
        if (ObjectUtil.isEmpty(day) && (ObjectUtil.isNotEmpty(year))) {
            prefix = prefix + formatDate(year, "yyyy");
        }
        if (ObjectUtil.isEmpty(day) && (ObjectUtil.isEmpty(year)) && (ObjectUtil.isNotEmpty(month))) {
            prefix = prefix + formatDate(month, "yyyy/MM");
        }


        Integer maxLimit = MapUtil.get(body, "maxLimit", Integer.class, 1000);
        String delimiter = MapUtil.get(body, "delimiter", String.class, "");
        String sysDictData = MapUtil.get(body, "sysDictData", String.class, "public");
        return fileService.getFileList(maxLimit, prefix, delimiter, sysDictData);

    }

    public static String formatDate(String inputDate, String outputFormat) {
        Instant instant = Instant.parse(inputDate);
        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.of("UTC"));

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(outputFormat);
        return dateTime.format(formatter);
    }

    @PostMapping("/selectByData")
    @ResponseBody
    public Result getFileListByData(@RequestBody Map<String, Object> body) throws Exception {
        File file = MapUtil.get(body, "file", File.class);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        String loginId = (String) tokenInfo.loginId;
        if (loginId == null) {
            return Result.error(403, "未登录，请重新登录");
        }
        return fileService.getFileListData(file, loginId);
    }


}
